package backstage.app.controller;

import java.time.LocalDateTime;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import backstage.app.entity.NoStudentType;
import backstage.app.mapper.NoStudentMapper;
import backstage.app.mapper.NoStudentTypeMapper;
import backstage.app.respont.RespontDateJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@RestController
@RequestMapping("${spring.application.name}/no-studentType/api")
@Api("非学员类型-控制器")
public class NoStudentTypeController {
	
	 
	@Autowired
	private NoStudentTypeMapper noStudentTypeMapper;
	@Autowired
	private NoStudentMapper noStudentMapper;
	@PostMapping("/list")
	@ApiOperation(value="获取集合类型", notes="")
	public List<NoStudentType> list() {
		List<NoStudentType>listStudenttype = null;
		try {
			 listStudenttype=noStudentTypeMapper.selectList(null);
			 for (NoStudentType noStudentType : listStudenttype) {
				int total= noStudentMapper.selectByPrimaryKeyCountType(noStudentType.getStudentType());
				noStudentType.setTotal(total);
			}
			 return  listStudenttype;
		} catch (Exception e) {
			// TODO: handle exception
			 return listStudenttype;
		}
	 
	}
	
	
	@PostMapping("/inser")
	@ApiOperation(value="添加标签")
	public RespontDateJson<?>inser(@RequestParam("name")String name){
		NoStudentType studenttype=noStudentTypeMapper.selectByNames(name);
		if(studenttype!=null) {
			return RespontDateJson.ok(studenttype, "此标签"+name+"已经存在数据库中了");
		}
		studenttype=new NoStudentType();
		studenttype.setNames(name);
		studenttype.setCreateTime(LocalDateTime.now());
		int colum=noStudentTypeMapper.insert(studenttype);
		int result=-1;
		if(colum>0) {
			studenttype.setStudentType(studenttype.getId());
			result=noStudentTypeMapper.updateByPrimaryKey(studenttype);
		}
		 
			
		return RespontDateJson.ok(result, "便签添加成功");
		
		 
	}
	
}
